<template>
  <div>
    <ych-modal
      :fullscreen="false"
      :title="title"
      :visible="visible"
      :destroyOnClose="true"
      @cancel="handleCancel">
      <a-form>

        <a-row>
          <a-col :span="24">
            <a-form-item :label="$t('m.eform.appview.entity.openType')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-model="openType" placeholder="" :allowClear="true">
                <a-select-option v-for="item in openTypeCol" :key="item.value" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row v-show="openType && needDialogSize.indexOf(openType) !== -1">
          <a-col :span="24">
            <a-form-item :label="$t('m.eform.appview.entity.dialogSize')"  :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-model="dialogSize" placeholder="" :allowClear="false">
                <a-select-option v-for="item in dialogSizeArr" :key="item.value" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row v-show="openType && noFormName.indexOf(openType) === -1">
          <a-col :span="24">
            <a-form-item :label="$t('m.eform.appform.entity.formName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input readOnly @click="selectForm" v-model="formName">
                <ych-icon slot="addonAfter" type="setting"/>
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-show="openType && noViewName.indexOf(openType) === -1">
          <a-col :span="24">
            <a-form-item :label="$t('m.eform.appview.entity.viewName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input readOnly @click="selectView" v-model="viewName">
                <ych-icon slot="addonAfter" type="setting"/>
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-show="openType && noObjectName.indexOf(openType) === -1">
          <a-col :span="24">
            <a-form-item :label="$t('m.eform.appform.entity.objectName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input readOnly @click="selectObject" v-model="objectName">
                <ych-icon slot="addonAfter" type="setting"/>
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row v-show="openType && needOutParameter.indexOf(openType) !== -1">
          <a-col :span="24">
            <a-form-item :label="$t('m.eform.appview.entity.outParameter')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-badge :dot="!!outParameter">
                <a-button type="link" size="small" @click="outParameterInput">{{ $t('m.common.button.config') }}</a-button>
              </a-badge>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row v-if="openType && needOpenScriptButton.indexOf(openType) !== -1">
          <a-col :span="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('m.eform.appview.entity.openScript')">
              <script-button v-model="openScript" :label="$t('m.common.button.config')" help-code="actionOpenScriptForJs"></script-button>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row v-if="openType">
          <a-col :span="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('m.eform.formDesign.label.qian-chu-li-jiao-ben')">
              <script-button v-model="beforeScript" :label="$t('m.common.button.config')" :help-code="isSubTable ? 'actionBeforeScriptForSubTable' : 'actionBeforeScriptForJs'"></script-button>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-if="openType">
          <a-col :span="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('m.eform.formDesign.label.hou-chu-li-jiao-ben')">
              <script-button v-model="afterScript" :label="$t('m.common.button.config')" :help-code="isSubTable ? 'actionAfterScriptForSubTable' : 'actionAfterScriptForJs'"></script-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <template slot="footer">
        <a-button key="back" @click="handleCancel">{{ $t('m.common.button.close') }}</a-button>
        <a-button key="submit" type="primary" @click="handleOk">{{ $t('m.common.button.ok') }}</a-button>
      </template>
    </ych-modal>

    <select-form-dialog ref="selectFormDialog" @selectFinished="selectFormOk" :groupId="groupId"></select-form-dialog>
    <select-view-all-dialog ref="selectViewDialog" @selectFinished="selectViewOk" :groupId="groupId"></select-view-all-dialog>
    <select-object-dialog ref="selectObjectDialog" @selectFinished="selectObjectOk" :groupId="groupId"></select-object-dialog>
    <out-parameter-dialog ref="outParameterDialog" @selectFinished="outParameterOk"></out-parameter-dialog>
  </div>
</template>

<script>

import {EFormConstant} from "../../EFormConstant";
import SelectFormDialog from "../../select/dialog/SelectFormDialog";
import SelectObjectDialog from "../../select/dialog/SelectObjectDialog";
import SelectViewAllDialog from "../../select/dialog/SelectViewAllDialog";
import OutParameterDialog from './OutParameterDialog'
import ScriptButton from "../designer/packages/ScriptButton";

export default {
  name: 'ActionConfigDialog',
  components: {
    ScriptButton,
    SelectFormDialog,
    SelectObjectDialog,
    SelectViewAllDialog,
    OutParameterDialog
  },
  props: {
    groupId: {
      type: String,
      required: true
    },
    isSubTable: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      needDialogSize: EFormConstant.needDialogSize,
      needOpenScriptButton: EFormConstant.needOpenScriptButton,
      needOutParameter: EFormConstant.needOutParameter,
      noFormName: EFormConstant.noFormName,
      noObjectName: EFormConstant.noObjectName,
      noViewName: EFormConstant.noViewName,
      openTypeCol: EFormConstant.openType.filter(vo => vo.canForLink && !this.isSubTable || vo.canForSubTable),
      dialogSizeArr: EFormConstant.dialogSize,
      title: this.$t('m.common.label.operate'),
      visible: false,
      record: {},
      openType: undefined,
      formName: null,
      formCode: null,
      viewName: null,
      viewCode: null,
      objectName: null,
      objectCode: null,
      outParameter: null,
      dialogSize: 'default',
      beforeScript: '',
      afterScript: '',
      openScript: '',
      labelCol: {
        xs: {span: 24},
        sm: {span: 4},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 18},
      },
    }
  },
  methods: {
    open(json, record) {
      this.record = record ? record : {}
      this.title = this.$t('m.eform.formDesign.label.dong-zuo-pei-zhi')
      this.title += this.record.colName ? ('——' + this.$locale(this.record.colName)) : ''

      const model = (json && json != '') ? JSON.parse(json) : {}
      this.openType = model.openType || undefined

      this.formName = model.formName || null
      this.formCode = model.formCode || null
      this.viewName = model.viewName || null
      this.viewCode = model.viewCode || null
      this.objectName = model.objectName || null
      this.objectCode = model.objectCode || null
      this.outParameter = model.outParameter || null
      this.dialogSize = model.dialogSize || 'default'

      this.beforeScript = model.beforeScript || ''
      this.afterScript = model.afterScript || ''
      this.openScript = model.openScript || ''

      this.visible = true;
    },
    handleOk() {
      if (this.openType) {
        const model = {}
        model.openType = this.openType
        if (this.noFormName.indexOf(this.openType) === -1) {
          if (this.formName) {
            model.formName = this.formName
          }
          if (this.formCode) {
            model.formCode = this.formCode
          }
        }
        if (this.noViewName.indexOf(this.openType) === -1) {
          if (this.viewName) {
            model.viewName = this.viewName
          }
          if (this.viewCode) {
            model.viewCode = this.viewCode
          }
        }
        if (this.noObjectName.indexOf(this.openType) === -1) {
          if (this.objectName) {
            model.objectName = this.objectName
          }
          if (this.objectCode) {
            model.objectCode = this.objectCode
          }
        }
        if (this.needOutParameter.indexOf(this.openType) !== -1) {
          if (this.outParameter) {
            model.outParameter = this.outParameter
          }
        }
        if (this.needOpenScriptButton.indexOf(this.openType) !== -1) {
          if (this.openScript) {
            model.openScript = this.openScript
          }
        }
        if (this.needDialogSize.indexOf(this.openType) !== -1) {
          if (this.dialogSize) {
            model.dialogSize = this.dialogSize
          }
        }
        if (this.beforeScript) {
          model.beforeScript = this.beforeScript
        }
        if (this.afterScript) {
          model.afterScript = this.afterScript
        }

        this.$emit('selectFinished', JSON.stringify(model))
      } else {
        this.$emit('selectFinished', '')
      }
      this.visible = false
    },
    handleCancel() {
      this.visible = false
    },
    selectForm() {
      this.$refs.selectFormDialog.radio = true
      this.$refs.selectFormDialog.visible = true
    },
    selectFormOk(selectedRowKeys, selectionRows) {
      this.formName = selectionRows ? selectionRows[0].formName : null
      this.formCode = selectionRows ? selectionRows[0].formCode : null
    },
    selectView() {
      this.$refs.selectViewDialog.radio = true
      this.$refs.selectViewDialog.visible = true
    },
    selectViewOk(selectedRowKeys, selectionRows) {
      this.viewName = selectionRows ? selectionRows[0].viewName : null
      this.viewCode = selectionRows ? selectionRows[0].viewCode : null
    },
    selectObject() {
      this.$refs.selectObjectDialog.radio = true
      this.$refs.selectObjectDialog.visible = true
    },
    selectObjectOk(selectedRowKeys, selectionRows) {
      this.objectName = selectionRows ? selectionRows[0].objName : null
      this.objectCode = selectionRows ? selectionRows[0].objCode : null
    },
    outParameterInput () {
      if(!this.viewCode){
        this.messageUtil.warning(this.$t('m.eform.appview.verify.link_page'))
        return
      }
      this.$refs.outParameterDialog.open(this.outParameter, this.viewCode)
    },
    outParameterOk (json) {
      this.outParameter = json
    },
  }
}
</script>
